package com.kebaijia.bio;

import java.io.*;
import java.net.ServerSocket;

import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author luoy
 * @date 2018/7/23 0023 下午 5:25
 * @Description: ${todo}
 */
public class BioServer {

    public static void main(String[] args) throws IOException {
       ServerSocket socket = new ServerSocket(8989);
        //服务器启动
        System.out.println("服务器启动");
        ExecutorService pool = Executors.newFixedThreadPool(10);


        while (true){
            final Socket accept = socket.accept();
            pool.submit(new Runnable() {
                public void run() {
                    try {
                        InputStream inputStream = accept.getInputStream();
                        DataInputStream dataInputStream = new DataInputStream(inputStream);
                        String s = dataInputStream.readUTF();
                        System.out.println("线程名字"+Thread.currentThread().getName()+" "+s);
                        accept.shutdownInput();


                        OutputStream outputStream = accept.getOutputStream();
                        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                        dataOutputStream.writeUTF("luoyuan");
                        accept.shutdownOutput();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
      /*  while (true){
            //接收客户端请求  每一个请求创建一个线程
           final Socket accept = socket.accept();
            new Thread(new Runnable() {
                public void run() {
                    try {

                        InputStream inputStream = accept.getInputStream();
                        DataInputStream dataInputStream = new DataInputStream(inputStream);
                        String s = dataInputStream.readUTF();
                        System.out.println("线程名字"+Thread.currentThread().getName()+" "+s);
                        accept.shutdownInput();


                        OutputStream outputStream = accept.getOutputStream();
                        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                        dataOutputStream.writeUTF("luoyuan");
                        accept.shutdownOutput();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();

        }*/
    }
}
